// pages/number/rental.js
import api from '../../utils/api'
import { numberRental, numberCheckin } from '../../utils/conf'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    let { id, house_property_id } = options;
    this.getData(id, house_property_id)
  },

  getData: function (id, house_property_id) {
    let that = this
    api.post(numberRental, {
      id: id,
      house_property_id: house_property_id
    })
      .then(res => {
        console.log(res)
        that.setData({
          list: res.data
        })
      }).catch(err => {
        console.log(err)
      })
  },

  bindDataChange(e) {
    let { key } = e.currentTarget.dataset
    let { value } = e.detail
    let data = this.data.list
    data[key] = value
    this.setData({
      list: data
    })
  },

  bindCheckin() {
    api.post(numberCheckin, this.data.list)
      .then(res => {
        // console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 1000,
          success: function () {
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/number/number'
              })
            }, 1000);
          }
        })
      }).catch(err => {
        console.log(err)
        wx.showToast({
          title: err.msg,
          icon: 'error',
          duration: 1000
        })
      })
  },

  bindDateChange: function (e) {
    let { value } = e.detail
    // console.log(value)
    let data = this.data.list
    data['checkin_time'] = value
    this.setData({
      list: data
    })
  },
})